import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import printerDetection.LinuxPrinterDetector;
import printerDetection.RandomPrinterDetector;
import printerDetection.PrinterDetector;


public class PrintMain{
  // the main frame of the application (top level container)
  private static final JFrame mainFrame = new JFrame("PrintOne");
  
  // the file chooser component
  private static final JFileChooser fc = new JFileChooser();
  
  // the file chooser text field
  private static JTextField fcText = new JTextField("Click browse to select a file", 20);
  
  // the list of printers available on the network
  private static String[] printers;
  
  // the printer chooser combo box
  private static JComboBox printerList;
  
  // the file to print
  private static File printFile = null;
  
  // the quit button
  private static JButton quit;

  // the browse button
  private static JButton browse;
  
  // the print button
  private static JButton print;
  
  // the find printer button
  private static JButton findPrinter;
  
  // the printer detector
  private static PrinterDetector printerDetector;
  
    
  /**
   * @param args
   */
  public static void main( String args[]){
    
  	// initialize the printer detector and do a printer detection first
  	printerDetector = new LinuxPrinterDetector();
  	printers = printerDetector.detectPrinters();
  	
  	// init the GUI
    initGUI();
  	
    // render the GUI
    renderGUI();
/*  	
  	Display display = new Display ();
  	Shell shell = new Shell(display);
  	shell.open ();
  	while (!shell.isDisposed ()) {
  		if (!display.readAndDispatch ()) display.sleep ();
  	}
  	display.dispose ();
*/
    
    

  }
  
  public static void initGUI(){

  	// Get the native look and feel class name 
  	//String nativeLF = UIManager.getSystemLookAndFeelClassName(); 

  	// Install the look and feel 
/*  	try { 
  		UIManager.setLookAndFeel(nativeLF); 
  	} catch (Exception e){
  		System.out.println("Installing look and feel is EVIL!");
  	}
*/
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.getContentPane().setLayout(new GridLayout(3,2));
    
    mainFrame.getContentPane().add(fcText);
    
    browse = addButton("Browse", new BrowseListener(), mainFrame.getContentPane());
    
    // init the printer list for the drop down
    printerList = new JComboBox(printers);
    printerList.setSelectedIndex(0);
    //printerList.addActionListener(this);
    mainFrame.getContentPane().add(printerList);
    
    findPrinter = addButton("Detect Printers", new DetectPrinterListener(), mainFrame.getContentPane());
    
    print = addButton("Print", new PrintListener(), mainFrame.getContentPane());
    
    quit = addButton("Quit", new QuitListener(), mainFrame.getContentPane());
    
  }
  
  private static void renderGUI(){
    mainFrame.pack();

    mainFrame.setResizable(false);
    
    mainFrame.setVisible(true);
  }

  
  private static JButton addButton(String label, ActionListener al, Container c){
  	JButton button = new JButton(label);
  	button.addActionListener(al);
  	c.add(button);
  	return button;
  }
  
  private static void addFileChooser(){
    //In response to a button click:
  	int returnVal = 0;
  	try{
  		returnVal = fc.showDialog(mainFrame, "Select");
  
    
  		if (returnVal == JFileChooser.APPROVE_OPTION){
  			// if the select button is clicked
  			// set the printFile to the selected file 
  			printFile = fc.getSelectedFile();
  			fcText.setText(printFile.getPath());
  		}
    
  	} catch (Exception e){
    	System.out.println("Something evil in showDialog");
    }
  }
  
  
  private static class BrowseListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent ae) {
      addFileChooser();
    }  
  }
  
  private static class PrintListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent ae) {
			// the print action is handled here
			printFile = new File(fcText.getText());
			if (printFile != null ){
				System.out.println("Print the file: " + printFile.getPath());
				try {
					Runtime.getRuntime().exec("lpr -P " +  printerList.getSelectedItem() + " " + printFile.getPath());
				} catch (Exception e){
					System.out.println("Running print is evil!");
				}
			}
			System.out.println("Use printer " + printerList.getSelectedItem() );
			
      /* here you can select a command to execute; I would put this in a separate class
      try{
        Runtime.getRuntime().exec("chmod +rwx " + fileName);
      } catch (IOException ioe){
        System.out.println("Something fishy happened");
      }*/
		}
  }
  
  private static class QuitListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			// just quit the application
			System.exit(0);
		}
  }
  
  private static class DetectPrinterListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent ae) {
			printers = printerDetector.detectPrinters();

			printerList.removeAllItems();
			
			for (String printer: printers)
				printerList.addItem(printer);
		}
  }
  
}
